<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="品牌名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入品牌名" :allowClear="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="logo" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">
              <j-image-upload :isMultiple="false" v-model="model.logo"
                              placeholder="请上传品牌logo图片"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea v-model="model.description" rows="4" placeholder="请输入介绍" :allowClear="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="显示状态" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="showStatus">
              <z-switch v-model="model.showStatus" :checked="'开'" :un-checked="'关'"></z-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="检索首字母" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstLetter">
              <a-input v-model="model.firstLetter" placeholder="请输入检索首字母" :allowClear="true"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import ZSwitch from '../../../components/compents/ZSwitch.vue'
import { postAction } from '../../../api/manage'

export default {
  name: 'PmsBrandForm',
  components: { ZSwitch },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, trigger: 'change', validator: this.verifyBrandName }],
        logo: [{ required: true, message: '请上传品牌logo图片！', trigger: 'change' }],
        description: [{ required: true, message: '请输入品牌介绍！', trigger: 'change' }],
        firstLetter: [{
          required: true,
          trigger: 'change',
          validator: this.verifyFirstLetter
        }]
      },
      url: {
        add: '/product/brand/add',
        edit: '/product/brand/edit',
        queryById: '/product/brand/queryById',
        verifyBrandName: '/product/brand/verifyBrandName'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    /**
     *校验首字母
     * @param rule
     * @param value
     * @param callback
     * @returns {*}
     */
    verifyFirstLetter(rule, value, callback) {
      if (value === undefined || value === '') {
        return callback(new Error('请输入品牌首字母！'))
      }
      if (value.length !== 1) {
        return callback(new Error('品牌首字母只能为一个大写字母！'))
      }
      if (!(/^[A-Z]+$/.test(value))) {
        return callback(new Error('品牌首字母只能为一个大写字母！'))
      }
      callback()
    },
    verifyBrandName(rule, value, callback) {
      if (value === undefined || value === '') {
        return callback(new Error('请输入品牌名称！'))
      }
      postAction(this.url.verifyBrandName, this.model).then(res => {
        if (res.success) {
          callback()
        } else {
          return callback(new Error(res.message))
        }
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>